package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.User;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/user/*")
public class UserServlet extends BaseServlet {
    UserService userService = BeanFactory.getBean("userService",UserService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //2 调用service层方法，查询所有数据
            List<User> list = userService.findAll();
            //3 响应结果
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //调用service层edit方法,执行删除操作
            userService.delete(ids);
            //响应客户端
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_USER_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            User user = BeanUtil.fillBeanFromJson(request, User.class);
            //调用service层edit方法,执行保存操作
            userService.update(user);
            //响应客户端
            return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_USER_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            String userId = request.getParameter("id");
            //调用service层方法,获取查询结果
            User user = userService.findById(userId);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            User user = BeanUtil.fillBeanFromJson(request, User.class);
            //调用service层save方法,执行保存操作
            userService.save(user);
            //响应客户端
            return new Result(true,MessageConstant.SAVE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.SAVE_USER_FAIL);
        }
    }

    //处理分页查询的请求
    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1.获取请求参数
            String strCurrentPage = request.getParameter("currentPage");
            String strPageSize = request.getParameter("pageSize");
            //1.1判断非空
            int currentPage = 1,pageSize = 5;
            if(!StringUtils.isBlank(strCurrentPage)){
                currentPage = Integer.parseInt(strCurrentPage);
            }
            if(!StringUtils.isBlank(strPageSize)){
                pageSize = Integer.parseInt(strPageSize);
            }
            //调用service层方法,获取查询结果
            PageInfo<User> pageInfo = userService.findByPage(currentPage,pageSize);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    //根据用户id更新角色,即给用户授权
    private Result updateUserAndRoles(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            //userId
            String userId = request.getParameter("userId");
            //checkedRoleIds
            String[] checkedRoleIds = BeanUtil.fillBeanFromJson(request, String[].class);
            //调用service层方法,根据用户绑定的角色也叫给用户授权
            userService.updateUserAndRoles(userId,checkedRoleIds);
            //对客户端做出响应
            return new Result(true, MessageConstant.SETTING_USER_AUTHORIZE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SETTING_USER_AUTHORIZE_FAIL);
        }
    }

    //查询登录的用户信息(用于页面展示)
    private Result findLoginUser(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.获取session域中的用户对象
            Object user = request.getSession().getAttribute("user");
            //2.对客户端做出响应
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
}
